package publisher;


import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.sql.DriverManager;



public class ARSListener implements MessageListener{

	private Connection connection;
	private Session session;
	private Topic smpRequestTopic;
	private MessageConsumer consumer;
	
	
	public static void main(String args[]){
		new ARSListener();
	}
	
	public ARSListener(){
		try
		{
		    Properties properties = new Properties();
		    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		    properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
		    properties.put(Context.PROVIDER_URL, "localhost");
		    
			InitialContext jndi = new InitialContext(properties);
			ConnectionFactory conFactory = (ConnectionFactory)jndi.lookup("XAConnectionFactory");
			connection = conFactory.createConnection();
			
			session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE );
			
			try
			{
				smpRequestTopic = (Topic)jndi.lookup("topic/arsTopic");
			}
			catch(NamingException NE1)
			{
				System.out.println("NamingException: "+NE1+ " : Continuing anyway...");
			}
			
			if( null == smpRequestTopic )
			{
				smpRequestTopic = session.createTopic("topic/arsTopic");
				jndi.bind("topic/arsTopic", smpRequestTopic);
			}
			
			consumer = session.createConsumer( smpRequestTopic );
			consumer.setMessageListener(this);
			System.out.println("Server started waiting for client requests");
			connection.start();
		}
		catch(NamingException NE)
		{
			System.out.println("Naming Exception: "+NE);
		}
		catch(JMSException JMSE)
		{
			System.out.println("JMS Exception: "+JMSE);
			JMSE.printStackTrace();
		}
	}

	@Override
	public void onMessage(Message msg) {
		
		MapMessage message = (MapMessage)msg;
		try {
			System.out.println("Why the hell???");
			java.sql.Connection  conn = null;
			CallableStatement stmt = null;
			try{
				
				String userName = "root";
		        String password = "root";
		        String url = "jdbc:mysql://localhost/ars1";
		        Class.forName ("com.mysql.jdbc.Driver").newInstance ();
		        conn = DriverManager.getConnection (url, userName, password);
		        System.out.println ("Database connection established");
		        stmt = conn.prepareCall("Call Postflightupdate (?,?,?)");
				stmt.setLong(1,message.getLong("flightId"));
				stmt.setString(2, message.getString("journeyDate"));
				stmt.setString(3, message.getString("message"));
				System.out.println(stmt);
				stmt.executeUpdate();
			}catch(Exception ex){
				ex.printStackTrace();
				
			}
			
			System.out.println(message.getString("message"));
			
		}  catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
